package cn.task.part1.service.impl;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import cn.task.part1.dao.DataObject;
import cn.task.part1.service.ReadAndCalculates;

public class ReadAndCalculatesImpl implements ReadAndCalculates{
	
	private DataObject dataObject;
	
	//缓存读出来的内容 
	private Map<String,String[]> map = new HashMap<>();
	
	public ReadAndCalculatesImpl() {
		dataObject = new DataObject();
		doReadCSV();
	}
	
	private void doReadCSV() {
		try(
				BufferedReader br = new BufferedReader(
						new InputStreamReader(
								new FileInputStream(dataObject.getFilepath())));
			) {
				String str;
				int count = 0;
				while((str=br.readLine())!=null) {
					String[] s = str.split(",");
					//缓存下所有的内容
					map.put(String.valueOf(count++), s);
					
					if(s[3].equals(dataObject.getFiveYear())) {
						dataObject.setFive(Integer.valueOf(s[2]).intValue());
						dataObject.setCount_five(1);
					}else if(s[3].equals(dataObject.getWithinFiveYear())) {
						dataObject.setWithin_five(Integer.valueOf(s[2]).intValue());
						dataObject.setCount_within_five(1);
					}
				}
				
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	}
	
	public void displayAll() {
		for(java.util.Map.Entry<String, String[]> entry:map.entrySet()) {
			System.out.println(Arrays.toString(entry.getValue()));
		}
	}
	
	public void displayCountAvg() {
		
		System.out.println("Five："+dataObject.getFive()/dataObject.getCount_five()+" within_five："
							+","+dataObject.getWithin_five()/dataObject.getCount_within_five());
	}

}
